package com.ipt.frm.ajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ipartek.formacion.dado.beans.Alumno;
import com.ipartek.formacion.dado.ddbb.DAOFactory;
import com.ipartek.formacion.dado.interfaces.InterfaceAlumnoDAO;

/**
 * Servlet implementation class AjaxListaAlumnos
 */
public class AjaxListaAlumnos extends HttpServlet {
	private static final long serialVersionUID = 1L;

	// ATRIBUTOS DE CONEXION
	private static DAOFactory myDao = DAOFactory
			.getDAOFactory(DAOFactory.MYSQL);

	// DAO ALUMNOS
	public static InterfaceAlumnoDAO alumnoDAO = myDao.getInterfaceAlumnoDAO();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AjaxListaAlumnos() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// Hay que especificar estos parametros antes de crear el out, para que
		// se le apliquen
		response.setContentType("text/html");
		response.setCharacterEncoding(StandardCharsets.UTF_8.toString());

		PrintWriter out = response.getWriter();

		String filtro = request.getParameter("filtro");

		ArrayList<Alumno> listaalumnos = alumnoDAO.getAlumnosFiltro(filtro);

		Iterator<Alumno> it = listaalumnos.iterator();

		String salida = "";

		while (it.hasNext()) {
			salida += "<option value='" + it.next().getNombreCompleto() + "'>";
		}

		out.print(salida);
	}
}
